package at.fhj.itm10.ticketmaster.helper;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

/**
 * 
 * @author Marvin Puchmüller
 * Sends Mail
 *
 */
public class MailSendingHelper implements MailSending {

	private long eventid = 0;
	private boolean success = false;
	private String content, subject = "";
	private Logger logger = Logger.getLogger(MailSendingHelper.class);
	private URL url = null;
	private String username = "";
	private String eventname = "";
	private String usergender = "";
	private String userLastname = "";
	private String eventdate = "";
	private String friendsemail = "marvin.puchmueller@hotmail.com";
	private String urlstring = "http://localhost:8080/Ticketmaster";
	
	/* (non-Javadoc)
	 * @see at.fhj.itm10.ticketmaster.helper.MailSending#sendMailToAll(java.util.List)
	 */
	public void sendMailToAll(List<String> recievers){
		for (String string : recievers) {
			try {
				sendMail(string);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	protected void sendMail(String reciever) throws IOException{
		Session session = null;
		try {
			session = MailHelper.getSession();
		} catch (Exception e1) {
			setSuccess(false);
			e1.printStackTrace();
		}
		try {
			
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(session
					.getProperty("mail.from")));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(reciever));
			message.setSubject(getSubject());
			message.setContent(getContent(), "text/html");
			Transport.send(message);
			setSuccess(true);
			logger.info(message.getContent());
			logger.info("Mail successfully sent");

		} catch (MessagingException e) {
			logger.error("Mail sending failed", e);
			setSuccess(false);
		}
	}
	
	public String initContent(){
		try {
			setSuccess(true);
			url = new URL(getUrlstring()+"/event.xhtml?id="
					+ getEventId());
			setContent(getUsergender() + " " + getUserLastname()
					+ " möchte Sie gerne einladen,<br/>" + "an dem Event "
					+ getEventname() + " am " + getEventdate()
					+ " teilzunehmen.<br/>"
					+ "Folgen Sie einfach dem Link:<br/>" + "<a href=\"" + url
					+ "\">http://localhost:8080/Ticketmaster/event.xhtml?id="
					+ getEventId() + "<\\a>");

			return getContent();
		} catch (MalformedURLException e) {
			logger.error(e);
			setSuccess(false);
			return "";
		}
	}
	
	public String initSubjectForUser(){
		setSubject("EventStyria.at - " + getUsername()
				+ " hat Ihnen eine Einladung geschickt!");
		return getSubject();
	}
	
	public String initContentforUser() {
		String usercontent;
		setContent(initContent());
		usercontent = getContent()
				.replace(
						"<a href=\""
								+ url
								+ "\">http://localhost:8080/Ticketmaster/event.xhtml?id="
								+ getEventId() + "<\\a>",
						"http://localhost:8080/Ticketmaster/event.xhtml?id="
								+ getEventId());
		usercontent = usercontent.replaceAll("<br/>", "\n");
		return usercontent;
	}
	
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getContent() {
		System.out.println("HGetter:"+ content);
		return content;
	}

	public void setContent(String content) {
		System.out.println("HSetter:"+ content);
		this.content = content;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public URL getUrl() {
		return url;
	}

	public void setUrl(URL url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEventname() {
		return eventname;
	}

	public void setEventname(String eventname) {
		this.eventname = eventname;
	}

	public String getUsergender() {
		return usergender;
	}

	public void setUsergender(String usergender) {
		this.usergender = usergender;
	}

	public String getUserLastname() {
		return userLastname;
	}

	public void setUserLastname(String userLastname) {
		this.userLastname = userLastname;
	}

	public String getEventdate() {
		return eventdate;
	}

	public void setEventdate(String eventdate) {
		this.eventdate = eventdate;
	}

	public String getFriendsemail() {
		return friendsemail;
	}

	public void setFriendsemail(String friendsemail) {
		this.friendsemail = friendsemail;
	}

	public long getEventId() {
		return eventid;
	}

	public void setEventId(long eventid) {
		this.eventid = eventid;
	}

	public String getUrlstring() {
		return urlstring;
	}

	public void setUrlstring(String urlstring) {
		this.urlstring = urlstring;
	}

	public void setHelper(MailSending helper) {
	}
	
	
}
